(function() {
    base2Cells.util.namespace('base2Cells.ytPlaylist');
    var ns = base2Cells.ytPlaylist;
    var prefs = null;
    var items = [];
    var Global = base2Cells.ytPlaylist.Global;
    base2Cells.ytPlaylist.Gadget = function() {
        if (gadgets) {
            prefs = new gadgets.Prefs();

            var unparsedItems = prefs.getString('ytList');

            if (unparsedItems) {
                items = eval('(' + gadgets.util.unescapeString(unparsedItems) + ')');
            }

            var container = new base2Cells.ytPlaylist.ItemContainer({
                itemList: items
            });

            container.applyTo('item-container');
            base2Cells.ytPlaylist.AddWindow.init('add-window');

            gadgets.util.registerOnLoadHandler(function() {

                if (wave.isInWaveContainer()) {
                    wave.setStateCallback(function() {
                        var state = wave.getState();
                        var changedItem = state.get('listChange');

                        if (Global.iAddedSong) {
                            Global.iAddedSong = false;
                            return true;
                        }

                        if (changedItem != null) {
                            try {
                                changedItem = eval('(' + changedItem + ')');
                            } catch(e) {
                                return true;
                            }

                            if (changedItem.add) {
                                try {
                                    if(Global.itemContainer.addSong(changedItem)) {
                                        base2Cells.ytPlaylist.addItem(changedItem);
                                    }
                                } catch(e) {
                                    alert("Could not add song.");
                                    state.submitValue('listChange', "");
                                }
                            } else if (changedItem.add == false) {
                                try {
                                    base2Cells.ytPlaylist.removeItem(changedItem);
                                    var itemContainer = Global.itemContainer;
                                    var itemList = itemContainer.itemList;
                                    for (var i = 0; i < itemList.length; i++) {
                                        if (itemList[i].id == changedItem.itemId) {
                                            itemList[i].removeSelf();
                                            itemList.splice(i, 1);
                                        }
                                    }

                                    base2Cells.ytPlaylist.removeItem(changedItem);
                                } catch(e) {
                                    state.submitValue('listChange', "");
                                }
                            }

                            state.submitValue('listChange', "");
                        }
                    });
                }

                if (wave.isInWaveContainer()) {
                    var state = wave.getState();

                    state.submitValue('listChange', "");
                    state.submitValue('ytList', wave.util.printJson(items));
                }
            });
        }
    }

    ns.addItem = function(item) {
        items.push(item);
        prefs.set("ytList", wave.util.printJson(items));
    };

    ns.removeItem = function(item) {
        for (var i = 0; i < items.length; i++) {
            if (items[i].artist == item.artist && items[i].title == item.title) {
                items.splice(i, 1);
            }
        }

        prefs.set("ytList", wave.util.printJson(items));
    };
})();
